define(['moment'], function (moment) {
    return ['$scope', '$rootScope', '$mdDialog', 'MMWService', '$filter', '$stateParams', '$timeout', 'config',
        function ($scope, $rootScope, $mdDialog, $MMWService, $filter, $stateParams, $timeout, config) {
            // 流程
            $scope.phases = [
                'process_tool',
                'process_scrap_reason'
            ];

            $scope.init = function(){
                $scope.user_name = config.cache.name;
                $scope.envInfo = $filter('translate')('Login.env.'+config.setting.environment);
                $scope.item_list = [];
                // 預設流程狀態
                $scope.phases_index = 0;
                
                // 自動隱蔽鍵盤
                element_barcode_no = document.getElementById('input_barcode');
                makeBarcodeInput(element_barcode_no);
                element_barcode_no.focus();
            };

            $scope.back = function(){
                if($scope.item_list.length > 0){
                    $scope.$root.showConfirm($filter('translate')('SMT13.msg.leave_but-data-no-save'), function(dialog){
                        dialog.hide();
                        $scope.$root.hideMenu();
                        $scope.$root.changeProgram('menu', true);
                    });
                } else {
                    $scope.$root.hideMenu();
                    $scope.$root.changeProgram('menu', true);
                };
            };

            // QRCode輸入
            $scope.QRCodeScanner = function(){
                $scope.$root.OpenScanner({
                    after : function(result){
                        input_barcode_no_callback(result.text);
                    },
                    exception: function(){
                        //目前裝置無法使用相機
                        $scope.$root.showAlert($filter('translate')('common.msg.cant_use_camera'));
                    },
                    orientation : 'portrait'
                });
            };

            // barcode輸入
            $scope.input_barcode = function(barcode){
                input_barcode_no_callback(barcode);
            };

            // 流程狀態
            $scope.PhaseStatus = function(index){
                if($scope.phases_index > index)
                    return ['prev'];
                else if($scope.phases_index == index)
                    return ['now'];
                else
                    return ['next'];
            };

            // 回上一步流程
            $scope.BackToPhase = function (index){
                if($scope.phases_index > index){
                    if(index == 0){
                        if($scope.item_list.length > 0){
                            $scope.$root.showConfirm($filter('translate')('SMT13.msg.leave_but-data-no-save'), function(dialog){
                                dialog.hide();
                                $scope.phases_index = index;
                                $scope.tool_class = '';
                                $scope.tool_no = '';
                                $scope.tool_state = '';
                                $scope.life_count = '';
                                $scope.acc_count = '';
                                $scope.item_list = [];
                            });
                        } else {
                            $scope.phases_index = index;
                            $scope.tool_class = '';
                            $scope.tool_no = '';
                            $scope.tool_state = '';
                            $scope.life_count = '';
                            $scope.acc_count = '';
                            $scope.item_list = [];
                        };
                    };
                };
            };

            // barcode輸入 callback
            function input_barcode_no_callback(barcode){
                if(barcode != ''){
                    if($scope.phases_index == 0){
                        // 工具流程
                        tool_info_get({
                            tool_no: barcode
                        }, function(data){
                            // 已上料 40 || 已上線 60 || 已上台車 50
                            if(data.state == '40' || data.state == '60' || data.state == '50'){
                                $scope.$root.showAlert($filter('translate')('SMT13.msg.this_tool_cannot_sendRepair', {state:$filter('translate')('SMT.common.state.s_'+data.state)}));
                            } else {
                                $scope.phases_index = 1;
                                $scope.tool_type_no = data.tool_type_no;
                                $scope.tool_class = data.tool_class;
                                $scope.tool_no = data.tool_no;
                                $scope.tool_state = data.state;
                                $scope.life_count = data.life_count;
                                $scope.acc_count = data.acc_count;
                            };
                        });
                    } else if($scope.phases_index == 1){
                        tool_abnormal_reason_info_get(barcode, function(itemList){
                            if(itemList && itemList > 0){
                                $scope.item_list.push(
                                    {
                                        reason_no: itemList[0].reason_no,
                                        reason_name: itemList[0].reason_name,
                                        memo: itemList[0].memo
                                    }
                                );
                            } else {
                                $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                            };
                        });
                    } else {
                        $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                    }
                }
            };

            // 開窗選取報廢原因
            $scope.AddReason = function () {
                $scope.$root.showQueryList({
                    key: 'reason_no',
                    header: [
                        {label:$filter('translate')('SMT13.list_scrap.scrap_reason'), code:'reason_name'},
                        {label:$filter('translate')('SMT13.list_scrap.remark'), code:'remark'}
                    ],
                    query: function(conditions, feedback){
                        tool_abnormal_reason_info_get('', function(itemList){
                            if(itemList && itemList.length > 0){
                                feedback(itemList);
                            } else {
                                $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                            };
                        });
                    },
                    confirm:function(selectItem, dialog){
                        dialog.hide();
                        var _temp = $filter('filter')($scope.item_list, {reason:selectItem.reason_no});
                        if(_temp && _temp.length == 0)
                            $scope.item_list.push({reason_no:selectItem.reason_no, reason_name:selectItem.reason_name, memo:''});
                    }
                });
            }

            // 刪除報廢原因項目
            $scope.RemoveReason = function (item) {
                $scope.item_list.splice($scope.item_list.indexOf(item), 1);
            };

            // 點擊確認按鈕送出資料
            $scope.confirm = function(){
                var content = {
                    tool_type_no: $scope.tool_type_no,
                    tool_no: $scope.tool_no,
                    tool_class: $scope.tool_class,
                    state: $scope.tool_state,
                    to_state: '80',
                    reason_list:$scope.item_list
                };
                tool_info_set_process(content, function(data){
                    $scope.item_list = [];
                    $scope.$root.showToast($filter('translate')('SMT13.msg.success'));
                    $scope.BackToPhase(0);
                });
            };

            // 取回 工具資訊
            function tool_info_get(content, callback){
                $MMWService.sendToServer({
                    uri : 'ServicesSMT.Module_SMT.tool_info_get',
                    content : content,
                    success : function(data){
                        if(callback)
                            callback(data);
                    }
                });
            }

            // 取得 工具異常原因
            function tool_abnormal_reason_info_get(reason_no, callback){
                var query_condition_info = [{column_name:'ReasonType', query_mode:'0', value: '1', merge_condition_model:'AND'}];
                if(reason_no && reason_no != '')
                    query_condition_info.push({column_name:'ReasonNo', query_mode:'0', value: reason_no, merge_condition_model:'AND'});

                $MMWService.pageNumberInfoGet({
                    page_mode : 'tool_abnormal_reason_info_get',
                    query_condition_info:query_condition_info
                }, function(itemList){
                    if(callback)
                        callback(itemList);
                });
            }

            // 送回資料
            function tool_info_set_process(content, callback){
                $MMWService.sendToServer({
                    uri : 'ServicesSMT.Module_SMT.tool_info_set_process',
                    content : content,
                    success : function(data){
                        if(callback)
                            callback(data);
                    }
                });
            };

            $scope.input_PDA_barcode = function(event){
                if (event.keyCode == 13) {
                    element_barcode_no.blur();
                    input_barcode_no_callback($scope.barcode)
                    $scope.barcode = '';
                } else {
                    $scope.barcode = $scope.barcode || '';
                }
            }

            //20200914 modify by Dustdusk for M#: 增加
            function makeBarcodeInput(el) {
                var _input = el.getElementsByClassName('barcode_input')[0];
                //var _mask = el.getElementsByClassName('barcode_mask')[0];
                el.focus = function () {
                    //e.preventDefault();
                    //_mask.click();
                    el.click();
                }
                el.addEventListener('click', function (e) {
                    e.preventDefault();
                    //_input.value = '';
                    if (document.activeElement !== _input)
                        _input.focus();
                });
                _input.addEventListener('focus', function (e) {
                    e.preventDefault();
                    setTimeout(function () {
                        _input.removeAttribute('readonly');
                    }, 200);
                });
                _input.addEventListener('blur', function (e) {
                    e.preventDefault();
                    _input.setAttribute('readonly', 'readonly');
                });
            }
        }
    ];
});
